/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.enumeration;

import com.huize.ladon.apm.common.enums.EnumInterface;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 描述: 时间单位(时间颗粒度)枚举
 *
 * @author tianyuliang
 * @version $Id: DateUnitEnum.java, v0.1 2020/4/7
 */
public enum DateUnitEnum implements EnumInterface {

    /**
     * 未知
     */
    UNKNOWN("unknown", 0L, "", "", "未知"),

    /**
     * 按分钟查看
     */
    MINUTE("minute", 60L, "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm", "分钟"),

    /**
     * 按小时查看
     */
    HOUR("hour", 3600L, "yyyy-MM-dd HH", "yyyy-MM-dd HH", "小时"),

    /**
     * 按天查看
     */
    DAY("day", 86400L, "yyyy-MM-dd", "yyyy-MM-dd", "天"),

    ;

    /**
     * 聚合指标
     */
    private String unit;

    /**
     * 转换秒秒
     */
    private Long conversion;

    /**
     * 聚合操作对应的SQL日期格式
     */
    private String sqlFormatter;

    /**
     * 聚合操作对应的Java日期格式
     */
    private String javaFormatter;

    /**
     * 聚合指标描述，对应前端页面：时间颗粒度 下拉菜单
     */
    private String desc;

    DateUnitEnum(String unit, Long conversion, String sqlFormatter, String javaFormatter, String desc) {
        this.unit = unit;
        this.conversion = conversion;
        this.sqlFormatter = sqlFormatter;
        this.javaFormatter = javaFormatter;
        this.desc = desc;
    }

    public static DateUnitEnum of(String type) {
        if (StringUtils.isBlank(type)) {
            return UNKNOWN;
        }
        return Stream.of(values()).filter(b -> b.getUnit().equalsIgnoreCase(type)).findFirst().orElseGet(() -> UNKNOWN);
    }

    /**
     * 获取所有类型列表
     *
     * @return
     */
    public static List<DateUnitEnum> all() {
        return Stream.of(values()).filter(typeEnum -> typeEnum != UNKNOWN).collect(Collectors.toList());
    }

    /**
     * 校验类型是否有效 true:有效, false:无效
     *
     * @param type
     * @return
     */
    public static boolean checkValidType(String type) {
        DateUnitEnum typeEnum = of(type);
        return typeEnum != UNKNOWN;
    }

    /**
     * 校验类型是否有效 true:有效, false:无效
     *
     * @param typeEnum
     * @return
     */
    public static boolean checkValidType(DateUnitEnum typeEnum) {
        return typeEnum != UNKNOWN;
    }

    public static boolean isMinuteType(DateUnitEnum typeEnum) {
        return typeEnum == MINUTE;
    }

    public static boolean isHourType(DateUnitEnum typeEnum) {
        return typeEnum == HOUR;
    }

    public static boolean isDayType(DateUnitEnum typeEnum) {
        return typeEnum == DAY;
    }

    public static boolean isDayType(String type) {
        return of(type) == DAY;
    }

    public String getUnit() {
        return unit;
    }

    public String getSqlFormatter() {
        return sqlFormatter;
    }

    public String getJavaFormatter() {
        return javaFormatter;
    }

    public Long getConversion() {
        return conversion;
    }

    @Override
    public String getCode() {
        return unit;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return String.format("[%s,%s,%s,%s,%s]", getCode(), getConversion(), getSqlFormatter(), getJavaFormatter(), getDesc());
    }

}
